package com.xjh.t.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.ClassLevel;
import com.xjh.t.domain.EnglishWord;
import com.xjh.t.domain.Lesson;
import com.xjh.t.domain.Sentence;
import com.xjh.t.service.IClassLevelService;
import com.xjh.t.service.IEnglishWordService;
import com.xjh.t.service.ILessonService;
import com.xjh.t.service.ISentenceService;
import com.xjh.t.vo.ClassLevelVo;
import com.xjh.t.vo.EnglishWordVo;
import com.xjh.t.vo.LessonVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/lesson")
public class LessonController {

    @Autowired
    private ILessonService lessonService;
    @Autowired
    private IClassLevelService classLevelService;

    @Autowired
    private IEnglishWordService englishWordService;

    @Autowired
    private ISentenceService sentenceService;

    @RequestMapping("/addlesson")
    public ResultObj addlesson(Lesson lesson) {

        QueryWrapper<Lesson> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("belong_level",lesson.getBelongLevel());
        queryWrapper.eq("lesson_num",lesson.getLessonNum());

        List<Lesson> list = this.lessonService.list(queryWrapper);

        if(list.size() > 0)
        {
            return ResultObj.ADD_ERROR;
        }

        this.lessonService.save(lesson);

        return ResultObj.ADD_SUCESS;

    }


    @RequestMapping("/loadAlllesson")
    public DataGridView loadAlllesson(LessonVo lessonVo) {

        IPage<Lesson> page=new Page<>(lessonVo.getPage(), lessonVo.getLimit());
        QueryWrapper<Lesson> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(lessonVo.getClassName()), "class_name",lessonVo.getClassName());
        //queryWrapper.eq(lessonVo.get() != null && classLevelVo.getLevelType() < 3, "level_type",classLevelVo.getLevelType());


        this.lessonService.page(page, queryWrapper);


        List<LessonVo> lessonVos = new ArrayList<>();
        for(Lesson lesson:page.getRecords())
        {
            LessonVo lessonVo1 = new LessonVo();
            BeanUtils.copyProperties(lesson,lessonVo1);

            ClassLevel classLevel = this.classLevelService.getById(lesson.getBelongLevel());

            lessonVo1.setBelongClass(classLevel.getLevelComment());

            lessonVos.add(lessonVo1);
        }

        return new DataGridView(page.getTotal(),lessonVos);

    }

    @RequestMapping("/updateLesson")
    public ResultObj updateLesson(Lesson lesson) {

        this.lessonService.updateById(lesson);

        return ResultObj.UPDATE_SUCESS;

    }

    @RequestMapping("/getLessons")
    public Object getLessons(Integer classlevelId) {

        QueryWrapper<Lesson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("belong_level",classlevelId);

        JSONObject json = new JSONObject();
        json.put("code",200);
        json.put("lessonlist",this.lessonService.list(queryWrapper));
        return json;

    }
    @RequestMapping("/getLessonWords")
    public Object getLessonWords(Integer lessonId) {

        QueryWrapper<EnglishWord> queryWrapper = new QueryWrapper<>();
        Lesson lesson = this.lessonService.getById(lessonId);
        List<EnglishWordVo> englishWordVos = new ArrayList<>();
        if(lesson != null) {

            String[] words = StringUtils.split(lesson.getLearnWords(),",");
            for(String word:words)
            {
                QueryWrapper<EnglishWord> englishWordQueryWrapper = new QueryWrapper<>();
                englishWordQueryWrapper.eq("english_word",word);

                List<EnglishWord> englishWords = this.englishWordService.list(englishWordQueryWrapper);
                if(englishWords.size() > 0) {
                    EnglishWordVo englishWordVo = new EnglishWordVo();
                    EnglishWord englishWord = englishWords.get(0);

                    BeanUtils.copyProperties(englishWord, englishWordVo);
                    List<Sentence> sentenceList = new ArrayList<>();
                    if (StringUtils.isNotBlank(englishWord.getSentenceIds())) {
                        String[] sentenceids = StringUtils.split(englishWord.getSentenceIds(), ",");
                        for (String senid : sentenceids) {
                            if (sentenceService.getById(Integer.parseInt(senid)) != null) {
                                sentenceList.add(sentenceService.getById(Integer.parseInt(senid)));
                            }
                        }
                    }
                    englishWordVo.setSentenceList(sentenceList);

                    englishWordVos.add(englishWordVo);
                }
            }

        }
       // queryWrapper.eq("belong_level",classlevelId);
        JSONObject json = new JSONObject();
        json.put("code",200);
        json.put("wordlist",englishWordVos);

        return json;

    }

}

